package com.sqstudio.astar
{
	

	/**
	 * Holds a two-dimensional array of Nodes methods to manipulate them, start node and end node for finding a path.
	 */
	public class Grid
	{
		private var _startNode:Node;
		private var _endNode:Node;
		private var _nodes:Array;
		private var _numX:int;//X方向格子数量
		private var _numY:int;//Y方向格子数量
		
		/**
		 * Constructor.
		 */
		public function Grid(xNum:int, yNum:int)
		{
			_numX = xNum;
			_numY = yNum;
			_nodes = new Array();
			
			for(var i:int = 0; i < _numY; i++)
			{
				_nodes[i] = new Array();
				for(var j:int = 0; j < _numX; j++)
				{
					_nodes[i][j] = new Node(j, i);
				}
			}
		}
		
		
		////////////////////////////////////////
		// public methods
		////////////////////////////////////////
		
		/**
		 * Returns the node at the given coords.
		 * @param x The x coord.
		 * @param y The y coord.
		 */
		public function getNode(x:int, y:int):Node
		{
			return _nodes[y][x] as Node;
		}
		
		/**
		 * Sets the node at the given coords as the end node.
		 * @param x The x coord.
		 * @param y The y coord.
		 */
		public function setEndNode(x:int, y:int):void
		{
			_endNode = _nodes[y][x] as Node;
		}
		
		/**
		 * Sets the node at the given coords as the start node.
		 * @param x The x coord.
		 * @param y The y coord.
		 */
		public function setStartNode(x:int, y:int):void
		{
			_startNode = _nodes[y][x] as Node;
		}
		
		/**
		 * Sets the node at the given coords as walkable or not.
		 * @param x The x coord.
		 * @param y The y coord.
		 */
		public function setWalkable(x:int, y:int, value:Boolean):void
		{
			_nodes[y][x].walkable = value;
		}
		
		
		
		////////////////////////////////////////
		// getters / setters
		////////////////////////////////////////
		
		/**
		 * Returns the end node.
		 */
		public function get endNode():Node
		{
			return _endNode;
		}
		
		/**
		 * Returns the number of columns in the grid.
		 */
		public function get numY():int
		{
			return _numY;
		}
		
		/**
		 * Returns the number of rows in the grid.
		 */
		public function get numX():int
		{
			return _numX;
		}
		
		/**
		 * Returns the start node.
		 */
		public function get startNode():Node
		{
			return _startNode;
		}

		public function get nodes():Array
		{
			return _nodes;
		}

		
	}
}